import 'package:intl/intl.dart';

/// 日期处理工具类
class DateUtil {
  /// 格式化日期时间
  static String formatDateTime(DateTime dateTime, {String pattern = 'yyyy-MM-dd HH:mm:ss'}) {
    final format = DateFormat(pattern);
    return format.format(dateTime);
  }

  /// 格式化日期
  static String formatDate(DateTime dateTime, {String pattern = 'yyyy-MM-dd'}) {
    final format = DateFormat(pattern);
    return format.format(dateTime);
  }

  /// 格式化时间
  static String formatTime(DateTime dateTime, {String pattern = 'HH:mm:ss'}) {
    final format = DateFormat(pattern);
    return format.format(dateTime);
  }

  /// 解析日期字符串
  static DateTime? parseDate(String dateStr, {String pattern = 'yyyy-MM-dd'}) {
    try {
      final format = DateFormat(pattern);
      return format.parseStrict(dateStr);
    } catch (e) {
      return null;
    }
  }

  /// 解析日期时间字符串
  static DateTime? parseDateTime(String dateStr, {String pattern = 'yyyy-MM-dd HH:mm:ss'}) {
    try {
      final format = DateFormat(pattern);
      return format.parseStrict(dateStr);
    } catch (e) {
      return null;
    }
  }

  /// 获取当前日期的开始时间 (00:00:00)
  static DateTime startOfDay(DateTime date) {
    return DateTime(date.year, date.month, date.day);
  }

  /// 获取当前日期的结束时间 (23:59:59)
  static DateTime endOfDay(DateTime date) {
    return DateTime(date.year, date.month, date.day, 23, 59, 59);
  }

  /// 计算两个日期之间的天数差
  static int daysBetween(DateTime from, DateTime to) {
    final fromDate = DateTime(from.year, from.month, from.day);
    final toDate = DateTime(to.year, to.month, to.day);
    return (toDate.difference(fromDate).inHours / 24).round();
  }

  /// 判断是否为今天
  static bool isToday(DateTime date) {
    final now = DateTime.now();
    return date.year == now.year && date.month == now.month && date.day == now.day;
  }

  /// 判断是否为昨天
  static bool isYesterday(DateTime date) {
    final yesterday = DateTime.now().subtract(const Duration(days: 1));
    return date.year == yesterday.year && date.month == yesterday.month && date.day == yesterday.day;
  }

  /// 判断是否为明天
  static bool isTomorrow(DateTime date) {
    final tomorrow = DateTime.now().add(const Duration(days: 1));
    return date.year == tomorrow.year && date.month == tomorrow.month && date.day == tomorrow.day;
  }

  /// 获取星期几
  static String getWeekday(DateTime date,
      {List<String> weekdays = const ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']}) {
    return weekdays[date.weekday % 7];
  }

  /// 计算年龄
  static int calculateAge(DateTime birthDate) {
    final currentDate = DateTime.now();
    int age = currentDate.year - birthDate.year;
    if (currentDate.month < birthDate.month ||
        (currentDate.month == birthDate.month && currentDate.day < birthDate.day)) {
      age--;
    }
    return age;
  }

  /// 添加天数
  static DateTime addDays(DateTime date, int days) {
    return date.add(Duration(days: days));
  }

  /// 添加月数
  static DateTime addMonths(DateTime date, int months) {
    return DateTime(date.year, date.month + months, date.day, date.hour, date.minute, date.second, date.millisecond,
        date.microsecond);
  }

  /// 添加年数
  static DateTime addYears(DateTime date, int years) {
    return DateTime(date.year + years, date.month, date.day, date.hour, date.minute, date.second, date.millisecond,
        date.microsecond);
  }

  /// 判断是否为闰年
  static bool isLeapYear(DateTime date) {
    final year = date.year;
    return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
  }

  /// 获取月份的天数
  static int daysInMonth(DateTime date) {
    final month = date.month;
    final year = date.year;

    if (month == 2) {
      return isLeapYear(date) ? 29 : 28;
    }

    const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    return daysInMonth[month - 1];
  }

  /// 格式化相对时间（如：刚刚、几分钟前、几小时前等）
  static String formatRelativeTime(DateTime dateTime) {
    final now = DateTime.now();
    final difference = now.difference(dateTime);

    if (difference.inSeconds < 60) {
      return '刚刚';
    } else if (difference.inMinutes < 60) {
      return '${difference.inMinutes}分钟前';
    } else if (difference.inHours < 24) {
      return '${difference.inHours}小时前';
    } else if (isToday(dateTime)) {
      return '今天 ${DateFormat('HH:mm').format(dateTime)}';
    } else if (isYesterday(dateTime)) {
      return '昨天 ${DateFormat('HH:mm').format(dateTime)}';
    } else {
      return DateFormat('yyyy-MM-dd HH:mm').format(dateTime);
    }
  }

  /// 获取日期范围
  static List<DateTime> getDateRange(DateTime startDate, DateTime endDate) {
    final List<DateTime> dates = [];
    DateTime currentDate = DateTime(startDate.year, startDate.month, startDate.day);
    final end = DateTime(endDate.year, endDate.month, endDate.day);

    while (currentDate.isBefore(end) || currentDate.isAtSameMomentAs(end)) {
      dates.add(currentDate);
      currentDate = currentDate.add(const Duration(days: 1));
    }

    return dates;
  }

  String duTimeLineFormat(DateTime dt) {
    var now = DateTime.now();
    var difference = now.difference(dt);

    // 1天内
    if (difference.inHours < 24) {
      return "${difference.inHours} hours ago";
    }
    // 30天内
    else if (difference.inDays < 30) {
      return "${difference.inDays} days ago";
    }
    // MM-dd
    else if (difference.inDays < 365) {
      final dtFormat = DateFormat('MM-dd');
      return dtFormat.format(dt);
    }
    // yyyy-MM-dd
    else {
      final dtFormat = DateFormat('yyyy-MM-dd');
      var str = dtFormat.format(dt);
      return str;
    }
  }
}
